package com.idlecode.service;

import cn.hutool.core.text.NamingCase;
import com.idlecode.bean.TableEntity;
import com.idlecode.config.GenDataSource;
import com.idlecode.config.query.AbstractQuery;
import com.idlecode.config.template.GeneratorConfig;
import com.idlecode.config.template.GeneratorInfo;
import com.idlecode.util.GenUtil;

import java.rmi.ServerException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: IdleCode
 * @description
 * @author: rongjuwei
 * @create: 2024-02-18 09:18
 **/
public class TableService {

    /**
     * 根据数据源，获取全部数据表
     *
     * @param dataSource 数据源
     */
    public static List<TableEntity> getTableList(GenDataSource dataSource, String dataBaseName) {
        List<TableEntity> tableList = new ArrayList<>();
        try {
            AbstractQuery query = dataSource.getDbQuery();
            String tableSql = query.tableSql(dataBaseName, null);
            //查询数据
            PreparedStatement preparedStatement = dataSource.getConnection().prepareStatement(tableSql);
            ResultSet rs = preparedStatement.executeQuery();
            while (rs.next()) {
                TableEntity table = new TableEntity();
                table.setTableName(rs.getString(query.tableName()));
                table.setTableComment(rs.getString(query.tableComment()));
                //table.setUpdateTime(rs.getString(query.tableUpdateTime()));
                table.setDatasourceId(dataSource.getId());
                tableList.add(table);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return tableList;
    }

    public static TableEntity getTableEntry(GenDataSource dataSource, String dataBaseName, String tableName,  List<String> selectedGenFileItems, boolean isFormatException) throws ServerException {
        GeneratorConfig generatorConfig = new GeneratorConfig();
        // 代码生成器信息
        GeneratorInfo generator = generatorConfig.getGeneratorConfig(selectedGenFileItems, isFormatException);
        // 从数据库获取表信息
        TableEntity table = getTable(dataSource, dataBaseName, tableName);
        // 保存表信息
        table.setPackageName(generator.getProject().getPackageName());
        table.setVersion(generator.getProject().getVersion());
        table.setBackendPath(generator.getProject().getBackendPath());
        table.setAuthor(generator.getDeveloper().getAuthor());
        table.setEmail(generator.getDeveloper().getEmail());
        //table.setGeneratorType(GeneratorTypeEnum.ZIP_DOWNLOAD.ordinal());
        table.setClassName(NamingCase.toPascalCase(tableName.substring(tableName.indexOf("_") + 1, tableName.length())));
        table.setModuleName(GenUtil.getModuleName(table.getTableName()));
        table.setFunctionName(GenUtil.getFunctionName(tableName));
        table.setCreateTime(new Date());
        return table;
    }

    /**
     * 根据数据源，获取指定数据表
     *
     * @param datasource 数据源
     * @param tableName  表名
     */
    public static TableEntity getTable(GenDataSource datasource, String dataBaseName, String tableName) throws ServerException {
        try {
            AbstractQuery query = datasource.getDbQuery();
            // 查询数据
            PreparedStatement preparedStatement = datasource.getConnection().prepareStatement(query.tableSql(dataBaseName, tableName));
            ResultSet rs = preparedStatement.executeQuery();
            if (rs.next()) {
                TableEntity table = new TableEntity();
                table.setTableName(rs.getString(query.tableName()));
                table.setTableComment(rs.getString(query.tableComment()));
                table.setDatasourceId(datasource.getId());
                return table;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        throw new ServerException("数据表不存在：" + tableName);
    }
}
